﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ueEngine.Biz.Inf.LogicEntity;
using ueEngine.Web.WebLogic.Model.SharingService;

namespace ueEngine.Web
{
    /// <summary>
    /// Cooperation 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Cooperation : System.Web.Services.WebService
    {
        [WebMethod(Description = "兼职信息查询，每页最大携带50条数据，页码从0页开始")]
        public List<Recruitment> Query(int page, int pagesize, AreaCode areaCode, PositionType positionType, out int itemCount)
        {
            pagesize = pagesize > 50 ? 50 : pagesize < 0 ? 0 : pagesize;
            page = page < 0 ? 0 : page;
            ItemListBox<RecruitSheetModel> res = new ueEngine.Biz.Core.AppService.Recruitment.RecruitService("SharingService")
                .QueryRecruitment(page, pagesize, RecruitSheetType.NoDefine, areaCode, positionType);
            itemCount = res.ItemTotle;
            return res.Data.Select(c => new Recruitment()
            {
                Address = c.WorkAddress,
                areaCode = c.WorkArea,
                Caption = c.Caption,
                ContactMan = c.Contact,
                ContactPhone = c.ContactPhone,
                ContactEmail = c.ReceiveEmail,
                PublishDate = c.PublishDate,
                QQ = c.QQ,
                Position = c.Positions.Select(p => new Position()
                {
                    Degree = p.Degree,
                    Description = p.PositionDescript,
                    Gender = p.Gender,
                    MaxSalary = p.MaxSalary,
                    MinSalary = p.MinSalary,
                    Period = p.Period,
                    PositionType = p.PositionType,
                    RequireExperience = p.Experienced,
                    Title = p.PositionName,
                    Vacancy = p.Vacancy
                }).ToList(),
                Company = new CompanyInfo()
                {
                    About = c.DepositCompany.CompanyDetail,
                    Business = c.DepositCompany.CompanyBusiness,
                    Dimensions = c.DepositCompany.CompanyDimensions,
                    Name = c.DepositCompany.CompanyName,
                    Type = c.DepositCompany.CompanyType
                }
            }).ToList();
        }
        [WebMethod(Description = "投递兼职信息。本条信息将纳入兼职招聘库中")]
        public BaseResult PushInfo(Recruitment recruitment, bool urgency)
        {
            string recruiter = Common.GetRecruiter();
            new ueEngine.Biz.Core.AppService.Recruitment.RecruitService(recruiter)
                .NewRecruit(new RecruitSheetModel()
                {
                    Caption = recruitment.Caption,
                    Contact = recruitment.ContactMan,
                    ContactPhone = recruitment.ContactPhone,
                    EnableEmail = !String.IsNullOrEmpty(recruitment.ContactEmail),
                    ReceiveEmail = recruitment.ContactEmail,
                    IsVerify = false,
                    PublishDate = DateTime.Now,
                    QQ = recruitment.QQ,
                    Recruiter = recruiter,
                    RecruitSubject = RecruitSheetSubject.Default,
                    RecruitType = RecruitSheetType.Normal,
                    Refresh = DateTime.Now,
                    Templete = false,
                    WorkAddress = recruitment.Address,
                    WorkArea = recruitment.areaCode,
                    DepositCompany = new DepositCompanyModel()
                    {
                        Agent = recruiter,
                        CompanyBusiness = recruitment.Company.Business,
                        CompanyDetail = recruitment.Company.About,
                        CompanyDimensions = recruitment.Company.Dimensions,
                        CompanyName = recruitment.Company.Name,
                        CompanyType = recruitment.Company.Type,
                        PostDate = DateTime.Now,
                        Templete = false
                    },
                    Positions = recruitment.Position.Select(c => new PositionModel()
                    {
                        Degree = c.Degree,
                        Experienced = c.RequireExperience,
                        Gender = c.Gender,
                        IsIndex = false,
                        MaxSalary = c.MaxSalary,
                        MinSalary = c.MinSalary,
                        Period = c.Period,
                        PositionDescript = c.Description,
                        PositionLevel = PositionLevel.Normal,
                        PositionName = c.Title,
                        PositionType = c.PositionType,
                        Priority = urgency ? PositionPriority.High : PositionPriority.Normal,
                        Vacancy = c.Vacancy
                    }).ToList()
                });
            return new BaseResult()
            {
                Success = true,
                Description = "我们已经成功接到您的信息，感谢您对" + ueEngine.Framework.Configuration.SiteResource.SiteName + "的支持。"
            };
        }
        [WebMethod(Description="合作企业登录")]
        public bool Login(string userName, string password)
        {
            var user = new ueEngine.Biz.Core.AppService.Member.UserService(userName).Login(password);
            return user != null && user.Role == MemberStatus.Firm;
        }
        [WebMethod(Description="获取企业信息")]
        public CorporationModel GetCorporation(string agent,string password)
        {
            var user = new ueEngine.Biz.Core.AppService.Member.UserService(agent).Login(password);
            if (user != null && user.Role == MemberStatus.Firm)
            {
                return user.CorporationInfo.Element(c => c.Agent == user.UserName);
            }
            return null;
        }
    }
    public static class Common
    {
        static int idx = 1;
        static string[] recruiter = { "51944179@qq.com", "214936150@qq.com", "1654625358@qq.com", "634691728@qq.com" };
        public static string GetRecruiter()
        {
            idx = idx == recruiter.Length ? 0 : idx + 1;
            return recruiter[idx % recruiter.Length];
        }
    }
}
